package activity

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Button
import android.widget.TextView
import com.hontech.kdxclient.R
import manager.AppManager
import org.angmarch.views.NiceSpinner

class InfoActivity : AppCompatActivity() {
    companion object {
        var isShow = false
    }

    private val arrayServices = listOf("测试服务器", "正式服务器")
    private val arrayPorts = listOf("ttyO2", "ttyO3", "ttyO4")

    private val textViewMac: TextView by lazy { findViewById<TextView>(R.id.id_info_text_view_mac) }
    private val spinnerServer: NiceSpinner by lazy { findViewById<NiceSpinner>(R.id.id_info_spinner_server) }
    private val spinnerRawPort: NiceSpinner by lazy { findViewById<NiceSpinner>(R.id.id_info_spinner_raw_port) }
    private val spinnerPosPort: NiceSpinner by lazy { findViewById<NiceSpinner>(R.id.id_info_spinner_pos_port) }
    private val buttonSetUp: Button by lazy { findViewById<Button>(R.id.id_info_button_setup) }
    private val buttonExit: Button by lazy { findViewById<Button>(R.id.id_info_button_exit) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_info)
        buttonSetUp.setOnClickListener(::onClickSetup)
        buttonExit.setOnClickListener(::onClickExit)
        initUi()
    }

    private fun onClickSetup(view: View) {
        when (spinnerServer.selectedIndex) {
            0 -> {
                AppManager.baseUrl = AppManager.TEST_SERVER_URL
            }

            1 -> {
                AppManager.baseUrl = AppManager.SERVER_URL
            }
        }

        /**
         * SN8不允许改串口
         */
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            return
        }

        when (spinnerRawPort.selectedIndex) {
            0 -> AppManager.deliverPortName = AppManager.PORT1
            1 -> AppManager.deliverPortName = AppManager.PORT2
            2 -> AppManager.deliverPortName = AppManager.PORT3
        }

        when (spinnerPosPort.selectedIndex) {
            0 -> AppManager.mdbPortName = AppManager.PORT1
            1 -> AppManager.mdbPortName = AppManager.PORT2
            2 -> AppManager.mdbPortName = AppManager.PORT3
        }
    }

    private fun onClickExit(view: View) {
        finish()
    }

    override fun onPause() {
        super.onPause()
        isShow = false
    }

    override fun onResume() {
        super.onResume()
        isShow = true
    }

    private fun initUi() {
        spinnerServer.attachDataSource(arrayServices)
        spinnerPosPort.attachDataSource(arrayPorts)
        spinnerRawPort.attachDataSource(arrayPorts)
        textViewMac.text = AppManager.MacAddress

        when (AppManager.baseUrl) {
            AppManager.SERVER_URL -> {
                spinnerServer.selectedIndex = 1
            }

            AppManager.TEST_SERVER_URL -> {
                spinnerServer.selectedIndex = 0
            }
        }

        when (AppManager.deliverPortName) {
            AppManager.PORT1 -> {
                spinnerRawPort.selectedIndex = 0
            }

            AppManager.PORT2 -> {
                spinnerRawPort.selectedIndex = 1
            }

            AppManager.PORT3 -> {
                spinnerRawPort.selectedIndex = 2
            }
        }

        when (AppManager.mdbPortName) {
            AppManager.PORT1 -> {
                spinnerRawPort.selectedIndex = 0
            }

            AppManager.PORT2 -> {
                spinnerRawPort.selectedIndex = 1
            }

            AppManager.PORT3 -> {
                spinnerRawPort.selectedIndex = 2
            }
        }
    }
}